home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15944 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: ix.netcom.com!netnews
  2. From: VanCLy@ix.netcom.com@ix.netcom.com   (Van Chinh Ly)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How do I put DIFFERENT classes in the same linked list?
  5. Date: 8 Apr 1996 06:44:35 GMT
  6. Organization: Netcom
  7. Message-ID: <4kackj$7is@reader2.ix.netcom.com>
  8. References: <4ka938$bj6@wintermute.ecs.fullerton.edu>
  9. Reply-To: VanCLy@ix.netcom.com
  10. NNTP-Posting-Host: sfo-ca11-12.ix.netcom.com
  11. X-NETCOM-Date: Sun Apr 07 11:44:35 PM PDT 1996
  12. X-Newsreader: IBM NewsReader/2 v1.2.5
  13.  
  14. In <4ka938$bj6@wintermute.ecs.fullerton.edu>, grosin@titan (Gil Rosin) writes:
  15. >...
  16. >
  17. >#include <iostream.h>
  18. >class A
  19. > {
  20. >  public:
  21. >    A *next;
  22. >    A *prev;
  23. >    void Print();
  24. > };
  25. >class Demo
  26. > {
  27. >  public:
  28. >    void Insert(A *Temp);
  29. >    void Insert2(A *Temp);
  30. >    void Test(void);
  31. >    A *First;
  32. > };
  33. >class B : public A
  34. > {
  35. >  void Print();
  36. > };
  37. >class C : public A
  38. > {
  39. >  void Print();
  40. > };
  41. >main()
  42. > {
  43. >  Demo *T = new Demo;
  44. >  A *Test = new B;
  45. >  T->Insert(Test);
  46. >  Test = new C;
  47. >  T->Insert2(Test);
  48. >  T->Test();
  49. >  return 0;
  50. > }
  51. >void B::Print()
  52. > {
  53. >  cout << "I am in class B" << '\n';
  54. > }
  55. >void C::Print()
  56. > {
  57. >  cout << "I am in class C" << '\n';
  58. > }
  59. >...
  60. >
  61. >Now, as you can see, I want to have a linked list that contains EITHER
  62. >B or C Items, but in the void Demo::Test() function, I want to be able to
  63. >just call them blindly with out knowing which is which. When I compile this
  64. >I get NO errors, but when I run it the two lines that print out are the
  65. >class A print function. It should print out the class B and then the class C.
  66. >
  67. >How can I do this? I am not worrying about constructors/destructors etc
  68. >right now, just worrying about how to get it to work.
  69. >
  70.  
  71. Is this homework?  :)
  72.  
  73. Check out "virtual functions."
  74.  
  75.  
  76. Van
  77.  
  78.